Skip to content

feat(code): harden branch linking during PR creation flow#1963

Merged
adboio merged 1 commit intomainfrom
04-30-feat_code_harden_branch_linking_during_pr_creation_flow
May 1, 2026
Merged

feat(code): harden branch linking during PR creation flow#1963
adboio merged 1 commit intomainfrom
04-30-feat_code_harden_branch_linking_during_pr_creation_flow

Conversation

@adboio
Copy link
Copy Markdown
Contributor

@adboio adboio commented Apr 30, 2026

Problem

The unified PR badge in the task header sometimes wouldn't render even though a PR existed — the simpler "View PR" + git options menu would show instead. Worse, it would only "fix itself" once the agent next edited a file.

The badge depends on workspace.linkedBranch being set. Linking happened via a fire-and-forget workspace.linkBranch.mutate() call in the renderer (useGitInteraction.runCreatePr) right after the create-PR mutation returned. If the renderer reloaded mid-flow (e.g. Vite HMR during dev, or any IPC tear-down), that second mutation never landed on the main process, the link was silently lost (only log.warn'd), and the only recovery was handleAgentFileActivity re-linking on the next agent edit.

#1947 made the happy path fast (subscription invalidation + optimistic getPrUrlForBranch cache prime) but didn't address the durability of the link itself — when the fire-and-forget call is dropped, neither leg of #1947 has anything to act on.

closes #1959

Changes

Move the linkBranch call into the main-process GitService.createPr flow so it's atomic with PR creation and survives renderer reloads.

  • Inject WorkspaceService into GitService (no circular dep — WorkspaceService doesn't depend on GitService).
  • After a successful PR creation, call workspaceService.linkBranch(taskId, branchName, "user"). Branch resolved as input.branchName ?? getCurrentBranch(directoryPath).
  • Drop both fire-and-forget linkBranch.mutate() blocks in useGitInteraction.runCreatePr. Kept invalidateGitBranchQueries and the optimistic getPrUrlForBranch cache prime — those still provide the latency win from fix(code): invalidate queries on PR creation #1947, just no longer load-bearing for correctness.
  • Updated GitService test instantiations to pass a WorkspaceService stub.

How did you test this?

  • pnpm --filter code typecheck clean
  • pnpm --filter code test — all 982 tests pass
  • Need to verify manually: create a PR via the UI, confirm the unified PR badge appears immediately, and that it still appears after a hot reload mid-flow.

Publish to changelog?

no

Copy link
Copy Markdown
Contributor Author

adboio commented Apr 30, 2026

@adboio adboio marked this pull request as ready for review April 30, 2026 21:33
@adboio adboio requested a review from a team April 30, 2026 21:33
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Apr 30, 2026

Prompt To Fix All With AI
Fix the following 1 code review issue. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 1
apps/code/src/main/services/git/service.ts:632-638
**No test coverage for the new `linkBranch` call in `createPr`**

The test file updates `GitService` constructors to accept a `WorkspaceService` stub (`{} as WorkspaceService`), but no test verifies that `workspaceService.linkBranch` is called (with the right args) during `createPr`. This is the core behaviour change of the PR, and `createPr` currently has zero test coverage. If a proper mock were supplied (e.g. `{ linkBranch: vi.fn() } as unknown as WorkspaceService`), it would also prevent a silent `TypeError` if any future test exercises a code path through `createPr` with a `taskId`.

Reviews (1): Last reviewed commit: "feat(code): harden branch linking during..." | Re-trigger Greptile

Comment on lines +632 to +638
if (input.taskId) {
const linkedBranch =
input.branchName ?? (await getCurrentBranch(directoryPath));
if (linkedBranch) {
this.workspaceService.linkBranch(input.taskId, linkedBranch, "user");
}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 No test coverage for the new linkBranch call in createPr

The test file updates GitService constructors to accept a WorkspaceService stub ({} as WorkspaceService), but no test verifies that workspaceService.linkBranch is called (with the right args) during createPr. This is the core behaviour change of the PR, and createPr currently has zero test coverage. If a proper mock were supplied (e.g. { linkBranch: vi.fn() } as unknown as WorkspaceService), it would also prevent a silent TypeError if any future test exercises a code path through createPr with a taskId.

Prompt To Fix With AI
This is a comment left during a code review.
Path: apps/code/src/main/services/git/service.ts
Line: 632-638

Comment:
**No test coverage for the new `linkBranch` call in `createPr`**

The test file updates `GitService` constructors to accept a `WorkspaceService` stub (`{} as WorkspaceService`), but no test verifies that `workspaceService.linkBranch` is called (with the right args) during `createPr`. This is the core behaviour change of the PR, and `createPr` currently has zero test coverage. If a proper mock were supplied (e.g. `{ linkBranch: vi.fn() } as unknown as WorkspaceService`), it would also prevent a silent `TypeError` if any future test exercises a code path through `createPr` with a `taskId`.

How can I resolve this? If you propose a fix, please make it concise.

@adboio adboio changed the base branch from 04-30-chore_code_remove_diff_agent_actions_flag to graphite-base/1963 May 1, 2026 15:24
@adboio adboio force-pushed the graphite-base/1963 branch from c4934c1 to bdd00c8 Compare May 1, 2026 15:24
@adboio adboio force-pushed the 04-30-feat_code_harden_branch_linking_during_pr_creation_flow branch from a74ccfc to 6b77dff Compare May 1, 2026 15:24
@graphite-app graphite-app Bot changed the base branch from graphite-base/1963 to main May 1, 2026 15:25
@adboio adboio force-pushed the 04-30-feat_code_harden_branch_linking_during_pr_creation_flow branch from 6b77dff to 1bb8a35 Compare May 1, 2026 15:25
@adboio adboio merged commit bae960e into main May 1, 2026
16 checks passed
Copy link
Copy Markdown
Contributor Author

adboio commented May 1, 2026

Merge activity

@adboio adboio deleted the 04-30-feat_code_harden_branch_linking_during_pr_creation_flow branch May 1, 2026 15:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

inconsistent PR status / git operation buttons

2 participants